import React, {Component} from 'react'
import {
  View,
  StyleSheet,
  TouchableOpacity,
  Text,
  Dimensions,
  Image, Clipboard
} from 'react-native'

import {Toast, Overlay, Label} from 'teaset'
import * as WeChat from 'react-native-wechat'
import * as token from './../../services/token'

const {
  height: deviceHeight,
  width: deviceWidth
} = Dimensions.get("window");


function shareTimeLine(props) {
  // Clipboard.setString(props.content);
  let shareId = token.getUserId() ? '?shareid='+token.getUserId() : '';
  WeChat.isWXAppInstalled()
    .then((isInstalled) => {
      if(isInstalled) {
        if(props.type && props.type === 'image'){
          WeChat.shareToTimeline({
            type: 'imageUrl',
            imageUrl: props.thumbImage,
          })
            .catch((error) => {
              Toast.fail(error.message);
            });
        }else {
          WeChat.shareToTimeline({
            type: 'news',
            description: props.description,
            thumbImage: props.thumbImage,
            title: props.title,
            webpageUrl: props.webpageUrl + shareId,
          })
            .catch((error) => {
              Toast.fail(error.message);
            });
        }
      }else{
        Toast.fail('没有安装微信软件，请您安装微信之后再试');
      }
    });
}


function shareSession(props) {
  let shareId
  if(props.cate === 'good'){
    shareId = token.getUserId() ? '&shareid='+token.getUserId() : '';
  }else{
    shareId = token.getUserId() ? '?shareid='+token.getUserId() : '';
  }
  WeChat.isWXAppInstalled()
    .then((isInstalled) => {
      if(isInstalled) {
        if(props.type && props.type === 'image'){
          WeChat.shareToSession({
            type: 'imageUrl',
            imageUrl: props.thumbImage,
          })
            .catch((error) => {
              Toast.fail(error.message);
            });
        }else{
          WeChat.shareToSession({
            type:'news',
            description: props.description,
            thumbImage: props.thumbImage,
            title: props.title,
            webpageUrl: props.webpageUrl+shareId,
          })
            .catch((error) => {
              Toast.fail(error.message);
            });
        }

      }else{
        Toast.fail('没有安装微信软件，请您安装微信之后再试');
      }
    });
}


export function ShareMenu(props){
  this.shareType = 'session'

  WeChat.addListener(
    'SendMessageToWX.Resp',
    (response) => {
      this.customPopView.close()
      console.log(response)
      if (parseInt(response.errCode) === 0) {
        props.type === 'image' && this.shareType === 'timeLine' ? Toast.message('分享成功,记得复制口令到朋友圈评论区哦！') :Toast.message('分享成功')
        // Toast.message('分享成功')
      } else {
        Toast.message('分享失败');
      }
    }
  );

  return(
    <Overlay.PullView side='bottom' modal={false}  ref={v => this.customPopView = v}>
      <View style={{backgroundColor: '#fff', minWidth: 300, minHeight: 160, justifyContent: 'center', alignItems: 'center'}}>
        <View style={styles.container}>
          <TouchableOpacity style={{justifyContent:'center', alignItems:'center', flex:1}} onPress={()=>{
            this.shareType = 'session'
            shareSession(props)
          }}>
            <View style={styles.shareImg}>
              <Image source={require('./../../../assets/images/session.png')} style={{width:45, height:45}}/>
            </View>
            <Text style={{fontSize:14, color:'#929292'}}>微信好友</Text>
          </TouchableOpacity>

          <TouchableOpacity style={{justifyContent:'center', alignItems:'center', flex:1}} onPress={()=>{
            this.shareType = 'timeLine'
            shareTimeLine(props)
          }}>
            <View style={styles.shareImg}>
              <Image source={require('./../../../assets/images/time.png')} style={{width:45, height:45}}/>
            </View>
            <Text style={{fontSize:14, color:'#929292'}}>朋友圈</Text>
          </TouchableOpacity>
        </View>
        <View style={styles.bottomContainer}>
          <TouchableOpacity style={styles.closeButton} onPress={()=>{
            this.customPopView && this.customPopView.close()}}>
            <Text style={{color:'#929292', fontSize:16}}>取消</Text>
          </TouchableOpacity>
        </View>
      </View>
    </Overlay.PullView>
  )
}


const styles = StyleSheet.create({
  container:{
    flex:1,
    justifyContent:'space-around',
    alignItems:'center',
    flexDirection:'row'
  },
  bottomContainer:{
    height:60,
    borderTopColor:'#ededed',
    borderTopWidth:1,
    width:deviceWidth
  },
  closeButton:{
    justifyContent:'center',
    alignItems:'center',
    height:50,
    width:deviceWidth
  },
  shareImg:{
    width:55,
    height:55,
    borderColor:'#ededed',
    borderWidth:1,
    borderRadius:50,
    justifyContent:'center',
    alignItems:'center',
    marginBottom:10
  }
})

